import { _decorator, Component, Node, ScrollBar, ScrollView, v2 } from 'cc';
import { LayoutViewHelper } from './LayoutViewHelper';
const { ccclass, property } = _decorator;

@ccclass('ScrollBarViewHelper')
export class ScrollBarViewHelper extends LayoutViewHelper {

    @property(ScrollView)
    scroll: ScrollView = null;

    @property
    speed: number = 2;

    onLoad(){
        this.selectedDefaultItem()
        super.onLoad()
    }


    public moveUp() {
        if (this.scroll.vertical) {
            let offset = this.scroll.getScrollOffset()
            let maxOffset = this.scroll.getMaxScrollOffset()
            offset.y = offset.y - this.speed;
            offset.y = offset.y < 0 ? 0 : offset.y;
            this.scroll.scrollToOffset(offset, 0)
            return true;
        } else {
            return false;
        }
    }

    public moveDown() {
        if (this.scroll.vertical) {
            let offset = this.scroll.getScrollOffset()
            let maxOffset = this.scroll.getMaxScrollOffset()
            offset.y = offset.y + this.speed;
            offset.y = offset.y > maxOffset.y ? maxOffset.y : offset.y;
            this.scroll.scrollToOffset(offset, 0)
            return true;
        } else {
            return false;
        }

    }

    public moveLeft() {
        if (this.scroll.vertical) {
            return false;
        } else {
            let offset = this.scroll.getScrollOffset()
            let maxOffset = this.scroll.getMaxScrollOffset()
            offset.x = offset.x - this.speed;
            offset.x = offset.x < 0 ? 0 : offset.x;
            this.scroll.scrollToOffset(offset, 0)
            return true;
        }

    }

    public moveRight() {
        if (this.scroll.vertical) {
            return false;
        } else {
            let offset = this.scroll.getScrollOffset()
            let maxOffset = this.scroll.getMaxScrollOffset()
            offset.x = offset.x + this.speed;
            offset.x = offset.x > maxOffset.x ? maxOffset.x : offset.x;
            this.scroll.scrollToOffset(offset, 0)
            return true;
        }
    }
}


